/**
 * ElectronExchanger Server Code File
 * 
 * File:        cliarg_helper.c
 * Brief:       BRIEF
 * Author:      Chen Kangbing
 * Email:       ckblau@outlook.com
 * Modified:    2020/09/08 17:05:24
 *
 */


#include "cliarg_helper.h"

#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>


void print_usage(char *progname) {
    printf("ElectronExchanger Server\n\n");
    printf("Usage: %s\n", progname);
    printf("\t -p (--port): Specify the port server would be listening on.\n");
    printf("\t -h (--help): Show help information.\n");
}

int parse_arg(ServerSocketTypeDef *srvdata, int argc, char **argv) {
    int ch;
    int port = 0;
    
    struct option opts[] = {{"port", required_argument, NULL, 'p'},
                            {"help", no_argument, NULL, 'h'},
                            {NULL, 0, NULL, 0}};

    while ((ch = getopt_long(argc, argv, "p:h", opts, NULL)) != -1) {
        switch (ch) {
        case 'p':
            port = atoi(optarg);
            break;
        case 'h':
            print_usage(argv[0]);
            return 1;
        }
    }

    if (!port) {
        print_usage(argv[0]);
        return -1;
    }

    srvdata->socket_port = port;
    return 0;
}